#include "Attribute.h"

using namespace std;

CAttribute::CAttribute(string attr)
{
	m_deliminator = "[],";
	Deserialize(attr);
}

CAttribute::CAttribute(string attr, string deliminator)
{
	m_deliminator = deliminator;
	Deserialize(attr);
}

CAttribute::~CAttribute()
{
	m_attrList.clear();
}

vector<string> CAttribute::List() 
{ 
	return m_attrList; 
}

int CAttribute::ReadAsInt(const std::string name)
{
	return m_ReadAsInt(name);
}

float CAttribute::ReadAsFloat(const string name)
{
	return m_ReadAsFloat(name);
}

vector<float> CAttribute::ReadAsVectorFloat(const std::string name, unsigned int nSize)
{
	return m_ReadAsVectorFloat(name, nSize);
}

vector<int> CAttribute::ReadAsVectorInt(const std::string name, unsigned int nSize)
{
	return m_ReadAsVectorInt(name, nSize);
}

string CAttribute::ReadAsString(const string name)
{
	return m_ReadAsString(name);
}

vector<string> CAttribute::ReadAsVectorStringXML(const std::string name)
{
	vector<string>	list;
	string			item;
	int				nSize = m_attrList.size();
	int				nCnt;

	for(nCnt = 0; nCnt < nSize; nCnt++)
	{
		if(m_attrList[nCnt] == name)
		{
			int nSCnt = 1;

			item.clear();

			do 
			{
				item.append(m_attrList[nCnt + nSCnt]);
				nSCnt++;

				if(nCnt + nSCnt >= nSize)
					break;

			}while(m_attrList[nCnt + nSCnt] != name);

			nCnt = nCnt + nSCnt;

			list.push_back(item);
		}
	}

	return list;
}

void CAttribute::Deserialize(const string attr)
{
	t_Tokenize(attr, m_deliminator);
}

int CAttribute::m_ReadAsInt(const std::string name)
{
	int		value;
	int     nCnt;
	int     nSize = m_attrList.size();

	for(nCnt = 0; nCnt < nSize; nCnt++)
	{
		if(m_attrList[nCnt] == name)
		{
			value = StringTo<int>(m_attrList[nCnt + 1]);
			return value;
		}
	}
	return 0;
}

float CAttribute::m_ReadAsFloat(const std::string name)
{
	float	value;
	int     nCnt;
	int     nSize = m_attrList.size();

	for(nCnt = 0; nCnt < nSize; nCnt++)
	{
		if(m_attrList[nCnt] == name)
		{
			value = StringTo<float>(m_attrList[nCnt + 1]);
			return value;
		}
	}
	return 0;
}

vector<float> CAttribute::m_ReadAsVectorFloat(const string name, unsigned int nSize)
{
	vector<float>	value;
	int				nCnt;
	int				nAttrSize = m_attrList.size();
	int				nVCnt;

	for(nCnt = 0; nCnt < nAttrSize; nCnt++)
	{
		if(m_attrList[nCnt] == name)
		{
			for(nVCnt = 0; nVCnt < nSize; nVCnt++)
				value.push_back(StringTo<float>(m_attrList[nCnt + 1 + nVCnt]));
			return value;
		}
	}
	return value;
}

vector<int> CAttribute::m_ReadAsVectorInt(const string name, unsigned int nSize)
{
	vector<int>	value;
	int			nCnt;
	int			nAttrSize = m_attrList.size();
	int			nVCnt;

	for(nCnt = 0; nCnt < nAttrSize; nCnt++)
	{
		if(m_attrList[nCnt] == name)
		{
			for(nVCnt = 0; nVCnt < nSize; nVCnt++)
				value.push_back(StringTo<int>(m_attrList[nCnt + 1 + nVCnt]));
			return value;
		}
	}
	return value;
}

string CAttribute::m_ReadAsString(const std::string name)
{
	int     nCnt;
	int     nSize = m_attrList.size();

	for(nCnt = 0; nCnt < nSize; nCnt++)
	{
		if(m_attrList[nCnt] == name)
			return m_attrList[nCnt + 1];
	}
	return 0;
}

void CAttribute::t_Tokenize(const string& str, const string& delimiters)
{
	// skip delimiters at beginning.
	string::size_type lastPos = str.find_first_not_of(delimiters, 0);

	// find first "non-delimiter".
	string::size_type pos = str.find_first_of(delimiters, lastPos);

	while(string::npos != pos || string::npos != lastPos)
	{
		// found a token, add it to the vector.
		m_attrList.push_back(str.substr(lastPos, pos - lastPos));

		// skip delimiters.  Note the "not_of"
		lastPos = str.find_first_not_of(delimiters, pos);

		// find next "non-delimiter"
		pos = str.find_first_of(delimiters, lastPos);
	}
}

vector<string> CAttribute::Tokenize(const string& str, const string& delimiters)
{
	// skip delimiters at beginning.
	string::size_type	lastPos = str.find_first_not_of(delimiters, 0);

	// find first "non-delimiter".
	string::size_type	pos = str.find_first_of(delimiters, lastPos);
	vector<string>		list;

	while(string::npos != pos || string::npos != lastPos)
	{
		// found a token, add it to the vector.
		list.push_back(str.substr(lastPos, pos - lastPos));

		// skip delimiters.  Note the "not_of"
		lastPos = str.find_first_not_of(delimiters, pos);

		// find next "non-delimiter"
		pos = str.find_first_of(delimiters, lastPos);
	}

	return list;
}


